package com.jms.login.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jms.login.po.Customer;
import com.jms.login.service.CustomerService;
import com.jms.login.utils.GetIP;

@Controller
public class CustomerController {
	@Autowired(required=true)
	@Resource(name="customerService")
	private CustomerService customerService;
	@Resource(name="pooledConnectionFactory")
	private PooledConnectionFactory factory;
	@Resource(name="queueDestination")
	private ActiveMQQueue queue;
	
	private String ip;
	private String name;
	private String pswd;
	private SimpleDateFormat df;
	private String timestamp;
	
	@RequestMapping("/loginCustomer")
	public String loginCustomer(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if(username.equals("")) {
			model.addAttribute("message","Please input username,Don't Empty!");
			return "show/loginCustomer";
		}
		if(password.equals("")) {
			model.addAttribute("message","Please input password,Don't Empty!");
			return "show/loginCustomer";
		}
		Customer customer = new Customer();
		customer.setUsername(username);
		customer.setPassword(password);
		Customer cus = customerService.login(customer);
		int tag = 0;
		if(cus != null) {
			tag = 1;
		}
		
		Connection connection = factory.createConnection();
		connection.start();
		
		Session sessionJMS = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
		MessageProducer producer = sessionJMS.createProducer(queue);
		TextMessage message = sessionJMS.createTextMessage();
		
		ip = GetIP.getIP(request);
		name = username;
		pswd = password;
		df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		timestamp = df.format(new Date());
		
		JSONObject json = new JSONObject();
		json.put("ip", ip);
		json.put("name", name);
		json.put("pswd", pswd);
		json.put("timestamp", timestamp);
		System.out.println(json.toString());
		message.setText(json.toString());
		producer.send(message);
		
		sessionJMS.commit();
		producer.close();
		sessionJMS.close();
		connection.close();
		
		if(tag == 0) {
			model.addAttribute("message","Please input Correct username And Password");
			return "show/loginCustomer";
		} else {
			model.addAttribute("message","Welcome to Our Online Restaurant");
			HttpSession session = request.getSession();
			session.setAttribute("username", customer.getUsername());
			session.setAttribute("customer", cus);
			return "show/loginSuccess";
		}
	}
	
}
